package org.hxyjs.dao.impl;

import org.hxyjs.dao.FocusOnTheTypeOfCompetitionDao;
import org.hxyjs.entity.FocusOnTheTypeOfCompetition;
import org.hxyjs.mapper.FocusOnTheTypeOfCompetitionMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository("focusOnTheTypeOfCompetitionDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名

public class FocusOnTheTypeOfCompetitionDaoImpl implements FocusOnTheTypeOfCompetitionDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private FocusOnTheTypeOfCompetitionMapper focusOnTheTypeOfCompetitionMapper;

    //查询全部
    public List<FocusOnTheTypeOfCompetition> getAllFocusOnTheTypeOfCompetitions() throws Exception {
        return focusOnTheTypeOfCompetitionMapper.selectByExample(null);
    }

    //按主键查询
    @Override
    public FocusOnTheTypeOfCompetition getFocusOnTheTypeOfCompetitionByFocno(String focno) throws Exception {
        return focusOnTheTypeOfCompetitionMapper.selectByPrimaryKey(focno);
    }

    //分页查询
    @Override
    public List<FocusOnTheTypeOfCompetition> getFocusOnTheTypeOfCompetitionByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) focusOnTheTypeOfCompetitionMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return focusOnTheTypeOfCompetitionMapper.getFocusOnTheTypeOfCompetitionByPage(map);
    }


    //删除
    @Override
    public Integer deleteFocusOnTheTypeOfCompetitionByFocno(String focno) {
        return focusOnTheTypeOfCompetitionMapper.deleteByPrimaryKey(focno);
    }

    //修改
    @Override
    public Integer updateFocusOnTheTypeOfCompetitionByFocno(FocusOnTheTypeOfCompetition focusOnTheTypeOfCompetition) {
        return focusOnTheTypeOfCompetitionMapper.updateByPrimaryKey(focusOnTheTypeOfCompetition);
}


    //增加
    @Override
    public Integer addFocusOnTheTypeOfCompetition(FocusOnTheTypeOfCompetition focusOnTheTypeOfCompetition) {
        focusOnTheTypeOfCompetition.setFocno(initId());
        return focusOnTheTypeOfCompetitionMapper.insertSelective(focusOnTheTypeOfCompetition);
    }

    public String initId() {
        String maxFocno = focusOnTheTypeOfCompetitionMapper.getMaxFocno();
        maxFocno = Optional.ofNullable(maxFocno).orElse("0000");
        return GenNumTools.initId("e", 4, maxFocno);
    }


}